//
//  FNNNZXJLCFMineViewControllerLCIDTb.m
//  Dvjf_ios
//
//  Created by Ji on 2018/9/6.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "FNNNZXJLCFMineViewControllerLCIDTb.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "WHIAxYJLCFMineModuleCellcFDrsU.h"
#import "MitSPaJLCFMineListCellSYnAJW.h"
#import "rmMqyfJLCFLoginAndRegisterControllerFbzYtj.h"
#import "uXwooCJLCFMineSettingViewControllerAUjeby.h"
#import "XZHNOcJLCFMineBindCardViewControllerMRXeuQ.h"
#import "vgxfqhJLCFMineTopUpViewControllerBVsVle.h"
#import "jExcVLJLCFMineWithdrawViewControllerpBjSQH.h"
#import "OsrffmJLCFMineBankCardViewControllerUidVai.h"
#import "ZaIMQHJLCFMineDateWebViewControllerfXCuLD.h"
#import "jKPhpTJLCFMineRiskWebViewControllerpzBXbX.h"
#import "empwxzJLCFMineAddressListViewControllerTeFNdn.h"
#import "UUfGtoJLCFMineAboutUsViewControlleraRZwfk.h"
#import "mFnyGKJLCFInviteWebViewControllertChoBH.h"
#import "wUGBczJLCFHomeMsgCenterControllerlMWSIn.h"
#import "ZCBVkpJLCFMineHeaderViewfzoLet.h"

@interface FNNNZXJLCFMineViewControllerLCIDTb ()

@property (nonatomic, strong) PBseiKJLCFMineDataModelWqnMcO *bnGwNcmineModelYfWNtI;
@property (nonatomic, strong) SssHPaJLCFMYCardInfoModelHkAXvP *OipDSPfullCardInfoUXjtNL;

@property (nonatomic, assign) BOOL shouldShowCard;

@property (nonatomic, strong) ZCBVkpJLCFMineHeaderViewfzoLet *IJMoQOmineHeaderViewOyGWLK;
@property (nonatomic, strong) UILabel *jyvhKwphoneLabelpopXEs;

@property (nonatomic, strong) NSMutableArray *imgArr;
@property (nonatomic, strong) NSMutableArray *yIBGmutitleArrEFwoZq;

@end

@implementation FNNNZXJLCFMineViewControllerLCIDTb

- (void)initParams {
    _imgArr = @[@"icon_mine_calendar", @"icon_mine_invite", @"icon_mine_risk", @"icon_mine_address", @"icon_mine_helpCenter", @"icon_mine_aboutUs", @"icon_mine_setting"].mutableCopy;
    _yIBGmutitleArrEFwoZq = @[@"回款日历", @"邀请好友", @"风险测试", @"收货地址", @"帮助中心", @"关于我们", @"设置"].mutableCopy;
    if (_shouldShowCard) {
        [_imgArr insertSafeObject:@"icon_mine_bankCard" atIndex:0];
        [_yIBGmutitleArrEFwoZq insertSafeObject:@"银行卡" atIndex:0];
    }
}

- (void)reloadTableView {
    [self initParams];
    [self.tableView reloadData];
}

- (void)registerNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self
                            selector:@selector(updateUserInfoUI)
                                name:KUserDetailInfoChange
                              object:nil];
}

- (ZCBVkpJLCFMineHeaderViewfzoLet *)IJMoQOmineHeaderViewOyGWLK {
    if (!_IJMoQOmineHeaderViewOyGWLK) {
        _IJMoQOmineHeaderViewOyGWLK = [[ZCBVkpJLCFMineHeaderViewfzoLet alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FitRealValue(620))];
        weakify(self);
        _IJMoQOmineHeaderViewOyGWLK.chargeCliced = ^{
            strongify(self);
            [self handleTopUp];
        };
        _IJMoQOmineHeaderViewOyGWLK.getMoneyClicked = ^{
            strongify(self);
            [self handleWithdraw];
        };
    }
    return _IJMoQOmineHeaderViewOyGWLK;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.frame = CGRectMake(0, 0, FULL_WIDTH, FULL_HEIGHT - TABBAR_HEIGHT);
    self.tableView.frame = CGRectMake(0, 0, FULL_WIDTH, self.view.height);
    self.tableView.mj_footer.hidden = YES;
    self.tableView.backgroundColor = QTZ_Gray237;
    self.tableView.tableHeaderView = self.IJMoQOmineHeaderViewOyGWLK;
    
    [self addTopNavi];
    
    [self updateUserInfoUI];
    
    [self registerNotification];
}

- (void)addTopNavi {
    
    [self.navigationBar removeFromSuperview];
    [super addNavigationBar];
    
    self.navigationBar.backgroundColor = [UIColor clearColor];
    [self.navigationBar setBottomBorderColor:[UIColor clearColor]];
    
    UIImageView *moreImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_mine_more"]];
    moreImg.left = 10;
    moreImg.centerY = NAVBAR_CENTER_Y;
    [self.navigationBar addSubview:moreImg];
    
    
    UILabel *jyvhKwphoneLabelpopXEs = [UILabel labelWithText:@"未登录" textColor:QTZ_White font:FONT(12)];
    jyvhKwphoneLabelpopXEs.left = moreImg.right + 7;
    jyvhKwphoneLabelpopXEs.centerY = moreImg.centerY;
    [self.navigationBar addSubview:jyvhKwphoneLabelpopXEs];
    _jyvhKwphoneLabelpopXEs = jyvhKwphoneLabelpopXEs;
    
    UIButton *settingImg = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingImg setImage:[UIImage imageNamed:@"icon_home_activity"] forState:UIControlStateNormal];
    settingImg.width = 40;
    settingImg.height = 40;
    settingImg.right = FULL_WIDTH - 14;
    settingImg.centerY = NAVBAR_CENTER_Y;
    settingImg.userInteractionEnabled = YES;
    [settingImg bk_whenTapped:^{
        [self handleMsgCenter];
    }];
    [self.navigationBar addSubview:settingImg];
}


- (void)updateUserInfoUI {
    [self endRefresh];
    self.bnGwNcmineModelYfWNtI = [JLCFRegisterService shareService].userInfo;
    [self.IJMoQOmineHeaderViewOyGWLK reloadHeaderInfoBy:[JLCFRegisterService shareService].userInfo];
    self.jyvhKwphoneLabelpopXEs.text = [JLCFRegisterService shareService].userInfo.phone?:@"未登录";
    [self.jyvhKwphoneLabelpopXEs sizeToFit];
    
    if ([JLCFRegisterService shareService].hasToken) {
        [sHkPtEJLCFMineRequestozAYZs myCardWithparams:nil loadingInView:self.view success:^(SssHPaJLCFMYCardInfoModelHkAXvP *data) {
            if ([[data isBind] isEqualToString:@"0"]) {
                self.shouldShowCard = NO;
                [self reloadTableView];
            } else if([[data isBind] isEqualToString:@"1"]){
                self.shouldShowCard = YES;
                self.OipDSPfullCardInfoUXjtNL = data;
                [self reloadTableView];
            }
        } failure:^(NSString *error) {
            self.shouldShowCard = NO;
            [self reloadTableView];
        }];
    } else {
        self.shouldShowCard = NO;
    }
    
    [self reloadTableView];
}

- (void)handleMsgCenter {
    if ([JLCFRegisterService shareService].hasToken) {
        [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:[wUGBczJLCFHomeMsgCenterControllerlMWSIn new] animated:YES];
    } else {
        rmMqyfJLCFLoginAndRegisterControllerFbzYtj *vc = [rmMqyfJLCFLoginAndRegisterControllerFbzYtj new];
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
    }
}

- (void)handleSetting {
    uXwooCJLCFMineSettingViewControllerAUjeby *settingVC = [uXwooCJLCFMineSettingViewControllerAUjeby new];
    settingVC.phone = self.bnGwNcmineModelYfWNtI.phone;
    settingVC.LogoutComplete = ^{
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] popViewControllerAnimated:YES];
    };
    [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:settingVC animated:YES];
}

- (void)handleTopUp {
    if ([JLCFRegisterService shareService].hasToken) {
            
        if (self.shouldShowCard) {
            MineTopUpViewController *vc = [MineTopUpViewController new];
            vc.nEvvdBfullCardDatakvLLBq = self.OipDSPfullCardInfoUXjtNL;
            [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:vc animated:YES];
        }else{
            XZHNOcJLCFMineBindCardViewControllerMRXeuQ *vc = [XZHNOcJLCFMineBindCardViewControllerMRXeuQ new];
            vc.flFGIcbindCardmirovY = YES;
            vc.isUPTOP = YES;
            [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
        }
    } else {
        [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:[rmMqyfJLCFLoginAndRegisterControllerFbzYtj new] animated:YES];
    }
}

- (void)handleWithdraw {
    if ([JLCFRegisterService shareService].hasToken) {
        [sHkPtEJLCFMineRequestozAYZs settingWithParams:nil loadingInView:nil success:^(bpXPwZJLCFMineSettingDataModelhmSxqV *data) {
            
            if ([data.type isEqualToString:@"1"]) {
                jExcVLJLCFMineWithdrawViewControllerpBjSQH *vc = [jExcVLJLCFMineWithdrawViewControllerpBjSQH new];
                vc.amount = self.bnGwNcmineModelYfWNtI.userMoney;
                [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:vc animated:YES];
            } else {
                XZHNOcJLCFMineBindCardViewControllerMRXeuQ *vc = [XZHNOcJLCFMineBindCardViewControllerMRXeuQ new];
                vc.flFGIcbindCardmirovY = YES;
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
            }
            
        } failure:^(NSString *error) {
            //            [[SRBApplicationEntrace shareEntrance].currentViewController showNotice:error];
        }];
    } else {
        [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:[rmMqyfJLCFLoginAndRegisterControllerFbzYtj new] animated:YES];
    }
}

- (void)loadData {
    if ([JLCFRegisterService shareService].hasToken) {
        [[JLCFRegisterService shareService] loadDetailUserInfo];
        
        [sHkPtEJLCFMineRequestozAYZs myCardWithparams:nil loadingInView:self.view success:^(SssHPaJLCFMYCardInfoModelHkAXvP *data) {
            if ([[data isBind] isEqualToString:@"0"]) {
                self.shouldShowCard = NO;
                [self reloadTableView];
            }else if([[data isBind] isEqualToString:@"1"]){
                self.shouldShowCard = YES;
                self.OipDSPfullCardInfoUXjtNL = data;
                [self reloadTableView];
            }
        } failure:^(NSString *error) {
            self.shouldShowCard = NO;
            [self reloadTableView];
        }];
    } else {
        [self updateUserInfoUI];
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    
    return _imgArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 100;
    } else {
        return 47;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WHIAxYJLCFMineModuleCellcFDrsU *cell = [WHIAxYJLCFMineModuleCellcFDrsU dequeueReusableCellForTableView:tableView];
        [cell reloadData];
        return cell;
    } else {
        MitSPaJLCFMineListCellSYnAJW *cell = [MitSPaJLCFMineListCellSYnAJW dequeueReusableCellForTableView:tableView];
        [cell setWithRow:indexPath.row imgName:_imgArr[indexPath.row] jsifqPtitleStrphUVlL:_yIBGmutitleArrEFwoZq[indexPath.row]];
        [cell reloadData];
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        
        NSString *jsifqPtitleStrphUVlL = _yIBGmutitleArrEFwoZq[indexPath.row];
        if ([JLCFRegisterService shareService].hasToken) {
            if ([jsifqPtitleStrphUVlL isEqualToString:@"银行卡"]) {
                OsrffmJLCFMineBankCardViewControllerUidVai *vc = [OsrffmJLCFMineBankCardViewControllerUidVai new];
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
            }
            
            if ([jsifqPtitleStrphUVlL isEqualToString:@"回款日历"]) {
                ZaIMQHJLCFMineDateWebViewControllerfXCuLD *webVC = [[ZaIMQHJLCFMineDateWebViewControllerfXCuLD alloc] init];
                webVC.url = QTZ_WEB_URL(@"date.html");
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
            }
            
            if ([jsifqPtitleStrphUVlL isEqualToString:@"邀请好友"]) {
                JLCFBaseWebViewController *webVC = [[JLCFBaseWebViewController alloc] init];
                webVC.url = QTZ_WEB_URL(@"share.html");
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
            }
            
            if ([jsifqPtitleStrphUVlL isEqualToString:@"风险测试"]) {
                jKPhpTJLCFMineRiskWebViewControllerpzBXbX *webVC = [[jKPhpTJLCFMineRiskWebViewControllerpzBXbX alloc] init];
                webVC.url = QTZ_WEB_URL(@"questionnaire.html");
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
            }
            
            if ([jsifqPtitleStrphUVlL isEqualToString:@"收货地址"]) {
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:[MineAddressgUCAmcJLCFListViewControllernRoqhZ new] animated:YES];
            }
            if ([jsifqPtitleStrphUVlL isEqualToString:@"设置"]) {
                [self handleSetting];
            }
            if ([jsifqPtitleStrphUVlL isEqualToString:@"帮助中心"]) {
                JLCFBaseWebViewController *webVC = [[JLCFBaseWebViewController alloc] init];
                webVC.url = QTZ_WEB_URL(@"help.html");
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
            }
            if ([jsifqPtitleStrphUVlL isEqualToString:@"关于我们"]) {
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:[UUfGtoJLCFMineAboutUsViewControlleraRZwfk new] animated:YES];
            }
        } else {
            if ([jsifqPtitleStrphUVlL isEqualToString:@"帮助中心"]) {
                JLCFBaseWebViewController *webVC = [[JLCFBaseWebViewController alloc] init];
                webVC.url = QTZ_WEB_URL(@"help.html");
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
            } else if ([jsifqPtitleStrphUVlL isEqualToString:@"关于我们"]) {
                [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:[UUfGtoJLCFMineAboutUsViewControlleraRZwfk new] animated:YES];
            } else {
                [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:[rmMqyfJLCFLoginAndRegisterControllerFbzYtj new] animated:YES];
                return;
            }
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *rRHNlxheaderViewfJQeRl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 10)];
    rRHNlxheaderViewfJQeRl.backgroundColor = [UIColor clearColor];
    return rRHNlxheaderViewfJQeRl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

@end
